package com.d3works.apps.account.service.payment.paymentvoucher;

import com.d3works.apps.account.db.PaymentVoucher;
import com.d3works.apps.account.db.repo.PaymentVoucherRepository;
import com.d3works.apps.account.exception.AccountExceptionMessage;
import com.d3works.exception.D3worksException;
import com.d3works.exception.db.repo.TraceBackRepository;
import com.d3works.i18n.I18n;
import com.d3works.meta.CallMethod;
import java.util.Objects;

public class PaymentVoucherToolService {

  /**
   * @param paymentVoucher : Une saisie Paiement
   *     <p>OperationTypeSelect 1 : Achat fournisseur 2 : Avoir fournisseur 3 : Vente client 4 :
   *     Avoir client
   * @return
   * @throws D3worksException
   */
  public boolean isDebitToPay(PaymentVoucher paymentVoucher) throws D3worksException {
    boolean isDebitToPay;

    switch (paymentVoucher.getOperationTypeSelect()) {
      case PaymentVoucherRepository.OPERATION_TYPE_SUPPLIER_PURCHASE:
        isDebitToPay = false;
        break;
      case PaymentVoucherRepository.OPERATION_TYPE_SUPPLIER_REFUND:
        isDebitToPay = true;
        break;
      case PaymentVoucherRepository.OPERATION_TYPE_CLIENT_SALE:
        isDebitToPay = true;
        break;
      case PaymentVoucherRepository.OPERATION_TYPE_CLIENT_REFUND:
        isDebitToPay = false;
        break;

      default:
        throw new D3worksException(
            paymentVoucher,
            TraceBackRepository.CATEGORY_MISSING_FIELD,
            I18n.get(AccountExceptionMessage.PAYMENT_VOUCHER_TOOL_1),
            paymentVoucher.getRef());
    }

    return isDebitToPay;
  }

  /**
   * @param paymentVoucher : Une saisie Paiement
   *     <p>OperationTypeSelect 1 : Achat fournisseur 2 : Avoir fournisseur 3 : Vente client 4 :
   *     Avoir client
   * @return
   * @throws D3worksException
   */
  public boolean isPurchase(PaymentVoucher paymentVoucher) throws D3worksException {

    boolean isPurchase;

    switch (paymentVoucher.getOperationTypeSelect()) {
      case PaymentVoucherRepository.OPERATION_TYPE_SUPPLIER_PURCHASE:
        isPurchase = true;
        break;
      case PaymentVoucherRepository.OPERATION_TYPE_SUPPLIER_REFUND:
        isPurchase = true;
        break;
      case PaymentVoucherRepository.OPERATION_TYPE_CLIENT_SALE:
        isPurchase = false;
        break;
      case PaymentVoucherRepository.OPERATION_TYPE_CLIENT_REFUND:
        isPurchase = false;
        break;

      default:
        throw new D3worksException(
            paymentVoucher,
            TraceBackRepository.CATEGORY_MISSING_FIELD,
            I18n.get(AccountExceptionMessage.PAYMENT_VOUCHER_TOOL_1),
            paymentVoucher.getRef());
    }

    return isPurchase;
  }

  @CallMethod
  public boolean isMultiCurrency(PaymentVoucher paymentVoucher) {
    return !Objects.equals(paymentVoucher.getCurrency(), paymentVoucher.getCompany().getCurrency());
  }
}
